package com.irun.pm.ui;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.utils.StringUtil;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 车辆信息
 * @author Evy
 *
 */
public class CarDetailsActivity extends BaseActivity implements OnClickListener{
	private DriverInfoControllers controllers;
	private DriverInfo driverInfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.car_details);
		controllers = new DriverInfoControllers(this);
		String id = getIntent().getStringExtra("id");
		driverInfo = controllers.getDriverInfo(id);//.queryDriverInfoById(id);
		if(driverInfo == null){
			Toast.makeText(this, "请求数据失败，请检查服务连接是否成功！", 3000).show();
			return;
		}
		((TextView)findViewById(R.id.tvName)).setText(StringUtil.getValue(driverInfo.getName()));
		((TextView)findViewById(R.id.tvCardNum)).setText(StringUtil.getValue(driverInfo.getCardNumber()));
		((TextView)findViewById(R.id.tvDate)).setText(StringUtil.getValue(driverInfo.getDate()));
		((TextView)findViewById(R.id.tvAddress)).setText(StringUtil.getValue(driverInfo.getAddress()));
		((TextView)findViewById(R.id.tvResult)).setText(StringUtil.getValue(driverInfo.getResult()));
		((TextView)findViewById(R.id.tvMachineType)).setText(StringUtil.getValue(driverInfo.getCard()));
		((TextView)findViewById(R.id.tvNative)).setText(StringUtil.getValue(driverInfo.getNativeAddress()));
		if(!StringUtil.isEmpty(driverInfo.getPhotoPath())){
			Bitmap bmp = BitmapFactory
					.decodeFile(driverInfo.photoPath);
			if(bmp == null)
				bmp = BitmapFactory.decodeResource(getResources(), R.drawable.default_photo);
			((ImageView)findViewById(R.id.ivCardDetailPhoto)).setImageBitmap(bmp);
		}
		findViewById(R.id.ivCardDetailPhoto).setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ivCardDetailPhoto:
			if(driverInfo!=null && !StringUtil.isEmpty(driverInfo.photoPath)){
				Intent intent = new Intent(this,PhotoView.class);
				intent.putExtra("path", driverInfo.photoPath);
				startActivity(intent);
			}else{
				Toast.makeText(getBaseContext(), "该记录未上传图片", Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}
	
	
}
